package com.zsr.orderservice.controller;


import com.zsr.commonutils.R;
import com.zsr.orderservice.service.TPayLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author zsr
 * @since 2021-06-22
 */
@Api(tags = {"支付日志接口"})
@RestController
//@CrossOrigin
@RequestMapping("/orderservice/paylog")
public class TPayLogController {

    @Autowired
    private TPayLogService payService;

    /**
     * 生成二维码
     *
     * @return
     */
    @ApiOperation(value = "生成wx二维码")
    @GetMapping("/createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo) {
        Map<String, Object> map = payService.createNative(orderNo);
        return R.ok().data(map);
    }

    //查询订单支付状态
    @ApiOperation(value = "查询wx支付状态")
    @GetMapping("/queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo) {
        //调用查询接口
        Map<String, String> map = payService.queryPayStatus(orderNo);
        if (map == null) {//出错
            return R.error().message("支付出错");
        }
        if (map.get("trade_state").equals("SUCCESS")) {//如果成功
            //更改订单状态
            payService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }

        return R.ok().code(25000).message("支付中");
    }

    @ApiOperation(value = "生成支付宝二维码")
    @GetMapping(value = "/createNativeByAlipay/{orderNo}")
    public R createNativeByAlipay(@PathVariable String orderNo) {
        Map<String, Object> map = payService.createNativeByAlipay(orderNo);
        return R.ok().data(map);
    }

    @ApiOperation(value = "查询支付宝支付状态")
    @GetMapping(value = "/queryPayStatusByAlipay/{orderNo}")
    public R queryPayStatusByAlipay(@PathVariable String orderNo) {
        Map<String, String> map = payService.queryPayStatusByAlipay(orderNo);
        if (map == null) return R.error().message("支付出错");
        if (map.get("trade_state") != null && map.get("trade_state").equals("TRADE_SUCCESS")) {//如果成功
            //更改订单状态
            payService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }

        return R.ok().code(25000).message("支付中");
    }


}

